package com.jz.beijianbao.price.controller;


import com.jz.beijianbao.beijian.bean.BeiJianSelectVo;
import com.jz.beijianbao.price.bean.PriceAdd;
import com.jz.beijianbao.price.bean.PriceSelectVo;
import com.jz.beijianbao.price.service.PriceService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class PriceController {

    @Autowired
    private PriceService priceService;


    //分页
    @PostMapping("materialCostPrice/queryPage")
    public ResultVO querypage(@RequestBody PriceSelectVo priceSelectVo){
        if(priceSelectVo.getPageIndex()==null){
            priceSelectVo.setPageIndex(1);
        }
        if(priceSelectVo.getPageSize()==null){
            priceSelectVo.setPageSize(10);
        }
        ResultVO resultVO = priceService.queryPage(priceSelectVo);
        return resultVO;
    }

    //备件选择框
    @PostMapping("material/searchEnabled")
    public ResultVO searchEnabled(@RequestBody @Valid BeiJianSelectVo spareSelectVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (spareSelectVo.getPageIndex()==null){
            spareSelectVo.setPageIndex(1);
        }
        if (spareSelectVo.getPageSize()==null){
            spareSelectVo.setPageSize(6);
        }
        return priceService.querySearchEnabled(spareSelectVo);
    }


    //新增
    @PostMapping("materialCostPrice/add")
    public ResultVO add(@RequestBody PriceAdd priceAdd){
        ResultVO resultVO = priceService.add(priceAdd);
        return resultVO;
    }
















}